<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/header.jsp" %>


	  <script type="text/javascript" src="${ctx}/assets/js/ztree/js/jquery.ztree.core-3.5.js"></script> 
	  <script type="text/javascript" src="${ctx}/assets/js/ztree/js/jquery.ztree.excheck-3.5.js"></script>
    <script type="text/javascript">
   var array = $.parseJSON('${jsonArray}');
   var setting = {
        check: {
            enable: true,
            chkboxType: {
                    "Y": "ps",
                    "N": "ps"
               }
           },
            data: {
                simpleData: {
                enable: true
                }
            }
        };
      //保存角色对应的菜单选项
         function saveRoleMenu(){
    	    
        	var treeObj = $.fn.zTree.getZTreeObj("menuTree");
        	var allNodes = treeObj.getNodes();
			var rnode = new Array();
			for(var i=0;i<allNodes.length;i++){
				var childNodes = allNodes[i].children;
				if(childNodes == null){
					 if(allNodes[i].checked==true &&rnode.indexOf(allNodes[i].id)==-1){
							rnode.push(allNodes[i].id);
						} 
					continue;
				}
				for(var j=0;j<childNodes.length;j++){
					var pId= childNodes[j].pId;
					if(childNodes[j].checked==true){
						rnode.push(childNodes[j].id);
						 if(pId !=null&&rnode.indexOf(pId)==-1){
							rnode.push(pId);
						} 
					}
				}
				
			}
			var param={roleId:$("#roleId").val(),menuId:rnode.toString()};
			
			 $.ajax({
	        		async : false,
	        		url: "${ctx}/role/saveRoleMenu.do",
	        		type:"post",
	        		data: param,
	        		dataType:"json",
	        		success: function(ret){	
	        			if(ret.status=='ok'){
	        				alert(ret.message);
	        				$('#roleMenus').dialog('close');
	        			}else{
	        				alert(ret.message);
	        			}					
	        		},
	        		error: function(ret){
	        			alert("保存异常！");
	        		}
	        	});
        }
      
      function resetMenu(){
    	  $.fn.zTree.init($("#menuTree"), setting, resultToNodes(array));
      }
      
        var menuIcon = "${ctx}/assets/js/ztree/css/zTreeStyle/img/diy/fMenu.png", 
        menuOpen = "${ctx}/assets/js/ztree/css/zTreeStyle/img/diy/open.png", 
        childMenu = "${ctx}/assets/js/ztree/css/zTreeStyle/img/diy/leaf.png";
        
      //分割子节点
        function resultToNodes(result){
          	var fNodes = [];
         	$.each(result,function(i,node){
         		fNodes.push(getNodes(node));
         	});
          	return fNodes;
        }
        //设置子节点信息
          function getNodes(node){
          	var fNode = {
          		id: node.id, //节点编号
          		pId: node.pId, //父节点编号
          		name:node.text,//节点名称（在机构树上展示的名称）
          		isParent: node.hasChild==0?false:true,//是否含有子节点
          		checked:node.isChecked,
        		chkDisabled:node.disabled,
        		iconOpen: menuOpen, //节点图标
        		iconClose: menuIcon,//节点图标
        		icon: childMenu // 节点图标
          	};
          	return fNode;
          }
      //加载树结构
       var scripts = [null, null];
        $('.page-content-area').ace_ajax('loadScripts', scripts, function() {
            $.fn.zTree.init($("#menuTree"), setting, resultToNodes(array));
        });
    </script>

<div class="row">
	<div class="col-xs-12">
		<div class="row">
			<div class="col-sm-12">
				<div class="widget-box widget-color-blue2">
					<div class="widget-body">
						<div class="widget-main padding-8">
						<input type="hidden" name="roleId" id="roleId" value="${roleId }" />
							<ul id="menuTree" class="ztree"></ul>
						</div>
					</div>
				</div>
			</div>	
		</div>
	</div><!-- /.col -->
</div>
 
